package com.yang.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.dao.entity.CategoryDO;
import com.yang.dao.mapper.CategoryMapper;
import com.yang.dto.req.CategorySaveReqDTO;
import com.yang.dto.resp.CategoryQueryRespDTO;
import com.yang.service.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分类业务实现层
 * 作者：孤城暖阳y
 * 时间：2025-03-11
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryDO> implements CategoryService {

    private final CategoryMapper categoryMapper;

    @Override
    public List<CategoryQueryRespDTO> queryCategories() {
        List<CategoryDO> categoryDOList = categoryMapper.selectList(null);
        return categoryDOList.stream()
                .map(entity -> BeanUtil.copyProperties(entity, CategoryQueryRespDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void saveCategory(CategorySaveReqDTO requestParam) {
        categoryMapper.insert(BeanUtil.copyProperties(requestParam, CategoryDO.class));
    }
}
